package com.infore.exchange.main.controller;


import com.infore.common.fw.BaseController;
import com.infore.common.model.dto.ChartsVo;
import com.infore.common.model.entity.vo.exchange.TExTransformLogVo;
import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiListResponse;
import com.infore.exchange.main.service.StatisticalService;
import com.infore.exchange.main.service.TDatabaseInfoService;
import com.infore.exchange.main.service.TExTransformLogService;
import com.infore.exchange.main.service.TTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api(value = "统计数据前端控制器", tags = "统计数据前端控制器")
@RequestMapping("/statistical")
public class StatisticalDataController extends BaseController {

    @Autowired
    private TDatabaseInfoService TDatabaseInfoService;


    @Autowired
    private TExTransformLogService TExTransformLogService;


    @Autowired
    private TTaskInfoService TTaskInfoService;

    @Autowired
    private StatisticalService statisticalService;

    @RequestMapping(value = "/rowNumAndTotalSpace", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "数据库记录行数与文件大小", notes = "数据库记录行数与文件大小")
    public ApiEntityResponse rowNumAndTotalSpace() {
        return ApiEntityResponse.ok(TDatabaseInfoService.getDatabaseRowNumAndTotalSpace());
    }

    @RequestMapping(value = "/dataTransformReadAndWriterRows", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "数据交换-数据发送与接收数量", notes = "数据交换-数据发送与接收数量")
    public ApiEntityResponse dataTransformReadAndWriterRows() {
        return ApiEntityResponse.ok(TExTransformLogService.queryDataTransformReadAndWriterRows());
    }


    @RequestMapping(value = "/schematabes", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "数据视图-数据库与数据表数量", notes = "数据交换-数据库与数据表数量")
    public ApiEntityResponse schematabes() {
        return ApiEntityResponse.ok(TDatabaseInfoService.getSchematabes());
    }

    @RequestMapping(value = "/taskSchedul", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "任务数及任务执行总次数", notes = "任务数及任务执行总次数")
    public ApiEntityResponse taskSchedul() {
        //任务个数
        int tasks = TTaskInfoService.selectCount(null);
        //执行总次数
        int taskExecuts = TExTransformLogService.selectCount(null);
        Map<String,Integer> map = new HashMap<>();
        map.put("tasks",tasks);
        map.put("taskExecuts",taskExecuts);
        return ApiEntityResponse.ok(map);
    }

    @RequestMapping(value = "/transformLogByLastday", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "最近天数的交换记录", notes = "最近天数的交换记录")
    public ApiListResponse<TExTransformLogVo> transformLogByLastday() {
        return ApiListResponse.ok(TExTransformLogService.queryTransformLogByLastday());
    }


    @RequestMapping(value = "/transformLogChart", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "数据交换时序图", notes = "数据交换时序图")
    public ApiEntityResponse<ChartsVo> transformLogChart(@RequestParam @ApiParam(required = true, name = "type", value = "类型(month，year)")  String  type) {
        ChartsVo chartsVo = null;
        if("month".equalsIgnoreCase(type)){
            chartsVo = TExTransformLogService.queryTransformLogReadWriterStatisticsOfLastMonth();
        }else if("year".equalsIgnoreCase(type)){
            chartsVo = TExTransformLogService.queryTransformLogReadWriterStatisticsOfLastYear();
        }
        return ApiEntityResponse.ok(chartsVo);
    }
    @RequestMapping(value = "/dataCollectionChart", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "采集量统计", notes = "采集量统计")
    public ApiEntityResponse<ChartsVo> dataCollectionChart(@RequestParam @ApiParam(required = true, name = "type", value = "类型(week，month)")  String  type) {
        ChartsVo chartsVo = null;
        if("week".equalsIgnoreCase(type)){
            chartsVo = statisticalService.queryDataCollectionStatisticsOfLastDay(7);
        }else if("month".equalsIgnoreCase(type)){
            chartsVo = statisticalService.queryDataCollectionStatisticsOfLastDay(30);
        }
        return ApiEntityResponse.ok(chartsVo);
    }
    @RequestMapping(value = "/dataCollectionRatio", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(value = "数据资源占比", notes = "数据资源占比")
    public ApiEntityResponse<Map<String,Object>> dataCollectionRatio() {
        return ApiEntityResponse.ok(statisticalService.queryDataCollectionRatio());
    }


}
